package com.xjnt.frameworks.core;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import com.xjnt.frameworks.exception.BusinessException;
import com.xjnt.frameworks.web.constants.SearchType;
import com.xjnt.frameworks.web.message.RetDataMsg;
import com.xjnt.frameworks.web.message.RetMsg;

public abstract class CRUDController<M extends BaseModel<M>, S extends BaseService<M>> extends BaseController {

	private static final String VIEW_PAGE = "list.html";
	private static final String ADD_PAGE = "add.html";
	private static final String EDIT_PAGE = "edit.html";

	protected abstract String getBizName();
	
	protected abstract Class<M> getClazz();

	protected abstract S getService();

	protected void indexBefore(){
		
	}
	
	/**
	 * 初始化加载,无需条件,显示所有分页数据
	 */
	public void index() {
		keepPara();
		indexBefore();
		render(VIEW_PAGE);
	}

	/**
	 * 初始化，分页通用。
	 */
	public void dataGrid() {
		// 分页信息
		int pageNumber = getParaToInt("pageNumber", 1);
		int pageSize = getParaToInt("pageSize", 10);
		// 搜索类型
		int searchType = getParaToInt("searchType", 1);
		SearchType sType = SearchType.General;
		if (searchType == 1) {
			sType = SearchType.Advanced;
		}
		// 获得条件 查询数据
		Map<String, String[]> paraMap = this.getParaMap();
		Page<M> page = getService().paginate(pageNumber, pageSize, paraMap, sType);
		RetDataMsg dataMsg = new RetDataMsg(true);
		dataMsg.setTotal(page.getTotalRow());
		dataMsg.setRows(JSON.parseArray(JsonKit.toJson(page.getList())));
		renderJson(dataMsg);
	}

	protected void addBefore(){
		
	}
	
	public void add() {
		keepPara();
		addBefore();
		setAttr(getService().getModel().getModelName(), getService().getModel());
		render(ADD_PAGE);
	}

	/**
	 * 对象保存
	 */
	public void doSave() {
		RetMsg retMsg = new RetMsg();
		try {
			M model = getModel(getClazz());
			boolean flag = getService().save(model);
			if (flag) {
				retMsg.pushOk("保存成功");
			} else {
				retMsg.pushError("保存失败");
			}
			renderJson(retMsg);
		} catch (Exception e) {
			throw new BusinessException("保存" + getBizName(), "发生异常：", e);
		}
	}

	public void editBefore(M model){
		
	}
	
	public void edit() {
		keepPara();
		M model = getService().findById(getPara("id"));
		editBefore(model);
		setAttr(model.getModelName(), model);
		render(EDIT_PAGE);
	}

	/**
	 * 数据更新
	 */
	public void doUpdate() {
		RetMsg retMsg = new RetMsg();
		try {
			M model = getModel(getClazz());
			boolean flag = getService().update(model);
			if (flag) {
				retMsg.pushOk("更新成功");
			} else {
				retMsg.pushError("更新失败");
			}
			renderJson(retMsg);
		} catch (Exception e) {
			throw new BusinessException("更新" + getBizName(), "发生异常：", e);
		}
	}

	/**
	 * 数据删除
	 */
	public void delete() {
		RetMsg retMsg = new RetMsg();
		try {
			String id = getPara("id");
			boolean flag = getService().deleteById(id);
			if (flag) {
				retMsg.pushOk("删除成功");
			} else {
				retMsg.pushError("删除失败");
			}
			renderJson(retMsg);
		} catch (Exception e) {
			throw new BusinessException("删除" + getBizName(), "发生异常：", e);
		}
	}

	/**
	 * 数据删除
	 */
	public void deletes() {
		RetMsg retMsg = new RetMsg();
		try {
			String idStr = getPara("ids");
			Object[] ids = idStr.split(",");
			boolean flag = getService().deleteByIds(ids);
			if (flag) {
				retMsg.pushOk("批量删除成功");
			} else {
				retMsg.pushError("批量删除失败");
			}
			renderJson(retMsg);
		} catch (Exception e) {
			throw new BusinessException("批量删除" + getBizName(), "发生异常：", e);
		}
	}
}
